/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.portlet.bean.impl;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.web.portlet.visitor.PortletVisitor;

/**
 * This Portlet represents a Hold in a Template.
 */
public class HolePortlet
    extends AbstractLinkedPortlet
{

  /**
   * <code>ThreadLocal</code> that contains the current name of the portlet
   * that must be insert in the hole this class represents.
   */
  private final transient ThreadLocal<String> currentPortletNames = new ThreadLocal<String>();

  /**
   * the name of the hole.
   */
  private String holeName;



  /**
   * Indicates whether some other object is "equal to" this one.
   * 
   * @param obj
   *          the reference object with which to compare.
   * @return <code>true</code> if this object is the same as the obj argument;
   *         <code>false</code> otherwise.
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(final Object obj)
  {
    if (obj instanceof HolePortlet) {
      return ObjectUtils.nullSafeEquals(getHoleName(), ((HolePortlet) obj).getHoleName());
    }
    return false;
  }



  /**
   * getter for the holeName property.
   * 
   * @return the holeName
   */
  public String getHoleName()
  {
    return holeName;
  }



  /**
   * returns the spring name of the linked portlet.
   * 
   * @return the spring name of the linked portlet
   */
  @Override
  public String getLinkedPortletName()
  {
    return currentPortletNames.get();
  }



  /**
   * Returns a hash code value for the object. This method is supported for the
   * benefit of hashtables such as those provided by
   * <code>java.util.Hashtable</code>.
   * 
   * @return a hash code value for this object.
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return ObjectUtils.nullSafeHashCode(getHoleName());
  }



  /**
   * remove the current name of the portlet that must be insert in the hole this
   * class represents.
   */
  public void removeCurrentPortletName()
  {
    currentPortletNames.remove();
  }



  /**
   * set the current name of the portlet that must be insert in the hole this
   * class represents.
   * 
   * @param portletName
   *          the current name of the portlet that must be insert in the hole
   *          this class represents
   */
  public void setCurrentPortletName(final String portletName)
  {
    currentPortletNames.set(portletName);
  }



  /**
   * returns whether this {@link LinkedPortlet} must keep in cash the sub-porlet
   * it uses.
   * 
   * @return whether this {@link LinkedPortlet} must keep in cash the sub-porlet
   *         it uses
   */
  @Override
  protected boolean doCashSubPortlet()
  {
    return false;
  }



  /**
   * setter for the holeName property.
   * 
   * @param holeName
   *          the holeName to set
   */
  public void setHoleName(final String holeName)
  {
    this.holeName = StringUtils.trimWhitespace(holeName);
  }



  /**
   * The visit method to use with a <code>PortletVisitor</code>.
   * 
   * @param visitor
   *          the <code>PortletVisitor</code>
   * @throws BundledException
   *           when an error occurred.
   * @see com.genia.toolbox.web.portlet.bean.Portlet#visit(com.genia.toolbox.web.portlet.visitor.PortletVisitor)
   */
  public void visit(final PortletVisitor visitor)
      throws BundledException
  {
    visitor.visitHolePortlet(this);
  }

}
